package com.bnyy.medicalHousekeeper.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bnyy.common.adapter.BaseNormalListAdapter;
import com.bnyy.medicalHousekeeper.GlideHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.activity.ArticleListActivity;
import com.bnyy.medicalHousekeeper.activity.ServiceGoodsDetailActivity;
import com.bnyy.medicalHousekeeper.bean.ServiceGoods;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.text.DecimalFormat;
import java.util.function.Consumer;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/9/29
 **/
public class ShopServiceGoodsAdapter extends BaseNormalListAdapter<ServiceGoods, ShopServiceGoodsAdapter.ViewHolder> {

    public ShopServiceGoodsAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.item_shop_service_goods, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        ServiceGoods serviceGoods = getData(position);

        holder.tvGoodsName.setText(serviceGoods.getName());
//        holder.tvPrice.setText("¥" + new DecimalFormat("0.00").format(serviceGoods.getPrice()));
        GlideHelper.setRoundImage(mContext, serviceGoods.getImg(), holder.iv);

        int type = serviceGoods.getGoods_type();
        holder.tagFlowLayout.setAdapter(new TagAdapter<String>(serviceGoods.getTags()) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = new TextView(mContext);
                tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                tv.setPadding(10, 5, 10, 5);
                tv.setTextColor(Color.parseColor(type == 1 ? "#41BE49" : "#FFA13D"));
                GradientDrawable bg = new GradientDrawable();
                bg.setCornerRadius(5);
                bg.setColor(Color.parseColor(type == 1 ? "#E8F6E7" : "#FFF9F2"));
                tv.setBackground(bg);
                tv.setText(s);

                return tv;
            }
        });
        holder.tvShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArticleListActivity.show(mContext, serviceGoods.getId(), serviceGoods.getGoods_type());
            }
        });
        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ServiceGoodsDetailActivity.show(mContext, serviceGoods.getId());
            }
        });
    }

    public static class ViewHolder extends BaseNormalListAdapter.ViewHolder {

        ImageView iv;
        TextView tvGoodsName;
        TextView tvPrice;
        TextView tvShare;
        TagFlowLayout tagFlowLayout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            iv = itemView.findViewById(R.id.iv);
            tvGoodsName = itemView.findViewById(R.id.tv_goods_name);
            tvPrice = itemView.findViewById(R.id.tv_price);
            tvShare = itemView.findViewById(R.id.tv_share);
            tagFlowLayout = itemView.findViewById(R.id.tag_flow_layout);
        }
    }
}
